package com.ichart.js;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ichart.doc.Info;
import com.ichart.utils.FileUtils;

public class ScriptCompiler {
	static void exec(String source,String dest) {
		StringBuffer cmd = new StringBuffer(
				"java -jar E:/ichart/compiler.jar --js  ");
		cmd.append(source);
		cmd.append(" --js_output_file ");
		cmd.append(dest);
		File f = new File(source);
		System.out.println("Input["+source+"]:"+ FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(f)));
		try {
			Process proc = Runtime.getRuntime().exec(cmd.toString());
			if (proc.waitFor() != 0)
				return;
			File t = new File(dest);
			
			/**
			 * 去掉换行
			 */
			FileUtils.write(t,FileUtils.readFileToString(t).replaceAll("\n",""));
			
			System.out.println("Out["+dest+"]:" + FileUtils.byteCountToDisplaySize(FileUtils.sizeOf(t)));
			System.out.println("compress rate:" + (f.length() - t.length()) * 100/ f.length() + "%");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	static void compressAll() {
		String toFile = Info.TargetPath +"ichart-"+Info.VERSION+".js";
		String toMinFile = Info.TargetPath +"ichart-"+Info.VERSION+".min.js";
		ScriptHelp.merge(toFile, false);
		exec(toFile,toMinFile);
	}

	static void minify() {
		for (String string : ScriptHelp.getJs()) {
			if(string.equalsIgnoreCase("ichart.coordinate.js"))
			exec(Info.SrcPath + string,Info.MinifyPath + string.replaceAll(".js", ".minify.js"));
		}
	}
	
	static void compressDist(List<String> source,String toFile) {
		toFile = Info.TargetPath +"dist/"+toFile;
		String Temp = Info.TargetPath +"dist/ichart.temp.js";
		ScriptHelp.mergeJS(source,Temp,false);
		exec(Temp,toFile);
		new File(Temp).delete();
	}
	
	static void compressDist(String[] source,String toFile) {
		List<String> list = new ArrayList();
		for(String string:source)
			list.add(string);
		compressDist(list,toFile);
	}
	
	static void compressDist(){
		compressDist(new String[]{
				"ichart.core.js",
				"ichart.element.js",
				"ichart.html.js",
				"ichart.painter.js",
				"ichart.component.js",
				"ichart.coordinate.js",
				"ichart.custom.js",
				"ichart.chart.js",
				"ichart.legend.js",
				"ichart.crosshair.js",
				"ichart.tip.js",
				"ichart.text.js"
				},"ichartjs.base.js");
		
		
		compressDist(new String[]{
				"ichart.linesegment.js",
				"ichart.line.js",
				"ichart.linebasic2d.js",
				"ichart.area2d.js"
				},"ichartjs.line.js");
		
		compressDist(new String[]{
				"ichart.sector.js",
				"ichart.sector2d.js",
				"ichart.sector3d.js",
				"ichart.label.js",
				"ichart.pie.js",
				"ichart.pie2d.js",
				"ichart.donut2d.js",
				"ichart.pie3d.js"
				},"ichartjs.pie.js");
		
		compressDist(new String[]{
				"ichart.rectangle.js",
				"ichart.rectangle2d.js",
				"ichart.rectangle3d.js",
				"ichart.column.js",
				"ichart.column2d.js",
				"ichart.column3d.js",
				"ichart.columnmulti2d.js",
				"ichart.columnmulti3d.js",
				"ichart.bar.js",
				"ichart.bar2d.js",
				"ichart.barmulti2d.js"
				},"ichartjs.column.js");
	}

	public static void main(String[] args) {
		compressAll();
		minify(); 
		compressDist();
		// WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, or ADVANCED_OPTIMIZATIONS
		// --compilation_level WHITESPACE_ONLY
		
		
	}
}
